package com.itsu.mongodb.service.impl;

import com.itsu.interfaces.entity.Comment;
import com.itsu.interfaces.service.CommentService;
import com.itsu.pojo.enums.CommentType;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.geo.Circle;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Long queryLikedCount(Object publishId) {
        return mongoTemplate.count(Query.query(Criteria.where("publishId").is(publishId)
                .and("commentType").is(CommentType.LIKED.getValue())
        ), Comment.class);
    }

    @Override
    public Long queryCommentCount(Object publishId) {
        return mongoTemplate.count(Query.query(Criteria.where("publishId").is(publishId)
                .and("commentType").is(CommentType.COMMENT.getValue())
        ), Comment.class);
    }

    @Override
    public Long queryLovedCount(Object publishId) {
        return mongoTemplate.count(Query.query(Criteria.where("publishId").is(publishId)
                .and("commentType").is(CommentType.LOVED.getValue())
        ), Comment.class);
    }

    //查看评论方式的状态
    @Override
    public Long queryCommentStatus(ObjectId publishId, Long userId, Integer commentType) {
        return mongoTemplate.count(Query.query(Criteria.where("publishId").is(publishId)
                .and("commentType").is(commentType)
                .and("userId").is(userId)
        ), Comment.class);
    }

    @Override
    public Long saveLoveComment(Comment comment) {
        mongoTemplate.save(comment);
        return queryLovedCount(comment.getPublishId());
    }

    @Override
    public Long deleteLoveComment(Comment comment) {
        mongoTemplate.remove(Query.query(Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType")
                        .is(comment.getCommentType())
                        .and("userId")
                        .is(comment.getUserId()))
                , Comment.class);
        return queryLovedCount(comment.getPublishId());
    }

    @Override
    public Long saveLikeComment(Comment comment) {
        mongoTemplate.save(comment);
        return queryLikedCount(comment.getPublishId());
    }

    @Override
    public Long deleteLikeComment(Comment comment) {
        mongoTemplate.remove(Query.query(Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType")
                        .is(comment.getCommentType())
                        .and("userId")
                        .is(comment.getUserId()))
                , Comment.class);
        return queryLikedCount(comment.getPublishId());
    }

    @Override
    public void saveComment(Comment comment) {
        mongoTemplate.save(comment);
    }

    @Override
    public void deleteComment(Comment comment) {
        mongoTemplate.remove(Query.query(Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType")
                        .is(comment.getCommentType())
                        .and("userId")
                        .is(comment.getUserId()))
                , Comment.class);
    }

    @Override
    public List<Comment> getComments(ObjectId publishId) {
        return mongoTemplate.find(Query.query(Criteria.where("publishId").is(publishId)
                        .and("commentType").is(CommentType.COMMENT.getValue()))
                , Comment.class);
    }

    @Override
    public List<Comment> getComments(Comment comment) {
        return mongoTemplate.find(Query.query(Criteria.where("publishUserId")
                        .is(comment.getPublishUserId())
                        .and("commentType").is(comment.getCommentType()))
                        .with(Sort.by(Sort.Order.desc("created")))
                , Comment.class);
    }

    @Override
    public List<Comment> getComments(ObjectId publishId, Integer commentType, String order) {
        Query query = new Query();
        query.addCriteria(Criteria.where("publishId").is(publishId).and("commentType").is(commentType));
        if ("desc".equalsIgnoreCase(order)) {
            query.with(Sort.by(Sort.Order.desc("created")));
        } else {
            query.with(Sort.by(Sort.Order.asc("created")));
        }
        return mongoTemplate.find(query, Comment.class);
    }

    @Override
    public Comment getComment(ObjectId commentId) {
        return mongoTemplate.findOne(Query.query(Criteria.where("id").is(commentId)), Comment.class);
    }

}
